//
//  AsyncImageBootcamp.swift
//  FCSwiftUI
//
//  Created by 冯宣超 on 2023/5/9.
//

import SwiftUI

struct AsyncImageBootcamp: View {
    let url = URL(string: "https://picsum.photos/400")
    var body: some View {
        AsyncImage(url: url) { phase in
            //这是文档推荐写法
//            if let image = phase.image {
//                image
//                    .resizable()
//                    .scaledToFit()
//                    .frame(width: 100, height: 100)
//                    .cornerRadius(20)
//            } else if phase.error != nil {
//                Image(systemName: "questionmark")
//                    .font(.headline)
//            } else {
//                ProgressView()
//            }
            
            switch phase {
            case .empty:
                ProgressView()
            case let .success(image):
                image
                    .resizable()
                    .scaledToFit()
                    .frame(width: 100, height: 100)
                    .cornerRadius(20)
            case .failure:
                Image(systemName: "questionmark")
                    .font(.headline)
            default:
                Image(systemName: "questionmark")
                    .font(.headline)
            }
        }
        
        AsyncImage(url: url, content: { image in
            image
                .resizable()
                .scaledToFit()
                .frame(width: 100, height: 100)
                .cornerRadius(20)
        }, placeholder: {
            ProgressView()
        })
    }
}

struct AsyncImageBootcamp_Previews: PreviewProvider {
    static var previews: some View {
        AsyncImageBootcamp()
    }
}
